package com.linkin.homepage.view.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.linkin.homepage.R;
import com.linkin.homepage.base.autolayout.layout.AutoFrameLayout;


public class RefreshFooterView extends AutoFrameLayout {

    private static final String TAG = "RefreshFooterView";
    private int mFooterState = RefreshFooterState.GONE;

    private View mGoneView;
    private View mLoadingView;
    private View mErrorView;
    private View mTheEndView;

    public RefreshFooterView(Context context) {
        this(context, null);
    }

    public RefreshFooterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mGoneView = findViewById(R.id.refresh_footer_gone_view);
        mLoadingView = findViewById(R.id.refresh_footer_loading_view);
        mErrorView = findViewById(R.id.refresh_footer_error_view);
        mTheEndView = findViewById(R.id.refresh_footer_end_view);
    }

    public boolean canLoadMore() {
        return mFooterState == RefreshFooterState.GONE || mFooterState == RefreshFooterState.ERROR;
    }

    public int getFooterState() {
        return mFooterState;
    }

    public void setGoneViewClickListener(OnClickListener listener) {
        if (mGoneView != null) {
            mGoneView.setOnClickListener(listener);
        }
    }

    public void setErrorViewClickListener(OnClickListener listener) {
        if (mErrorView != null) {
            mErrorView.setOnClickListener(listener);
        }
    }

    public boolean setFooterState(int footerState) {
        boolean viewChanged;
        switch (footerState) {
            case RefreshFooterState.LOADING:
                viewChanged = setViewVisible(mLoadingView);
                viewChanged = setViewGone(mGoneView) || viewChanged;
                viewChanged = setViewGone(mErrorView) || viewChanged;
                viewChanged = setViewGone(mTheEndView) || viewChanged;
                break;

            case RefreshFooterState.ERROR:
                viewChanged = setViewVisible(mErrorView);
                viewChanged = setViewGone(mGoneView) || viewChanged;
                viewChanged = setViewGone(mLoadingView) || viewChanged;
                viewChanged = setViewGone(mTheEndView) || viewChanged;
                break;

            case RefreshFooterState.EMPTY:
                viewChanged = setViewGone(mGoneView);
                viewChanged = setViewGone(mLoadingView) || viewChanged;
                viewChanged = setViewGone(mErrorView) || viewChanged;
                viewChanged = setViewGone(mTheEndView) || viewChanged;
                break;

            case RefreshFooterState.END:
                viewChanged = setViewVisible(mTheEndView);
                viewChanged = setViewGone(mGoneView) || viewChanged;
                viewChanged = setViewGone(mLoadingView) || viewChanged;
                viewChanged = setViewGone(mErrorView) || viewChanged;
                break;

            default:
                viewChanged = setViewVisible(mGoneView);
                viewChanged = setViewGone(mLoadingView) || viewChanged;
                viewChanged = setViewGone(mErrorView) || viewChanged;
                viewChanged = setViewGone(mTheEndView) || viewChanged;
                footerState = RefreshFooterState.GONE;
                break;
        }
        mFooterState = footerState;
        return viewChanged;
    }

    private boolean setViewGone(View view) {
        if (view != null) {
            if (view.getVisibility() != GONE) {
                view.setVisibility(GONE);
                return true;
            }
        }
        return false;
    }

    private boolean setViewVisible(View view) {
        if (view != null) {
            if (view.getVisibility() != VISIBLE) {
                view.setVisibility(VISIBLE);
                return true;
            }
        }
        return false;
    }
}
